import $http from "@/ajax/http.js";

export const business = {
	data() {
		return {
			roles: wx.getStorageSync('roles'),
			coll: [],
		}
	},
	onLoad() {
		this.getConfirmReceiveMoneyList();
	},
	methods: {
		async getConfirmReceiveMoneyList() {
			try {
				if (this.roles == 5) {
					await this.roleBus();
				} else if (this.roles == 6) {
					await this.roleStore();
				} else if (this.roles == 7) {
					await this.roleServer();
				} else if (this.roles == 4) {
					await this.roleCity();
				} else if (this.roles == 3) {
					await this.roleCause();
				}
			} catch (error) {
				console.error("在获取确认收款列表时出错:", error);
			}
		},

		// 业务合伙人
		roleBus() {
			$http.request({
				url: "/bus/index/getConfirmReceiveMoneyList",
				method: 'GET'
			}).then((res) => {
				this.coll = res
				console.log(res);
			})
		},

		// 事业合伙人
		roleCause() {
			$http.request({
				url: "/work/index/getConfirmReceiveMoneyList",
				method: 'GET'
			}).then((res) => {
				this.coll = res
				console.log(res);
			})
		},

		// 门店合伙人
		roleStore() {
			$http.request({
				url: '/store/my/getConfirmReceiveMoneyList',
				method: "GET",
			}).then((res) => {
				this.coll = res
				console.log(res, "确认收款");
			})
		},

		//服务合伙人
		roleServer() {
			$http.request({
				url: "/waiter/my/getConfirmReceiveMoneyList",
				method: 'GET'
			}).then((res) => {
				this.coll = res
				console.log(res);
			})
		},

		//城市合伙人
		roleCity() {
			$http.request({
				url: "/city/index/getConfirmReceiveMoneyList",
				method: 'GET'
			}).then((res) => {
				this.coll = res
				console.log(res);
			})
		},



		confirmUnBillList(id) {
			if (this.roles) {
				switch (this.roles) {
					case 6:
						this.store(id);
						break;
					case 5:
						this.business(id);
						break;
					case 7:
						this.server(id);
						break;
					case 4:
						this.city(id);
						break;
					case 3:
						this.cause(id);
						break;
					default:
						break;
				}
			}
		},

		confirmReceiveMoney(id, url) {
			wx.showModal({
				title: '确认收款',
				content: '您是否确认收款?',
				success: (res) => {
					if (res.confirm) {
						$http.request({
							url: url,
							data: {
								id: id
							},
							method: "POST",
						}).then((res) => {
							console.log(res, "点击确认收款");
							if (res.code == 200) {
								this.coll.forEach(item => {
									if (item.id === id) {
										item.confirmMoney = 1;
									}
								});
								wx.showToast({
									title: '您已确认收款',
									icon: 'success',
									duration: 2000
								});
							} else {
								wx.showToast({
									title: res.message || '确认收款失败，请重试',
									icon: 'none',
									duration: 2000
								});
							}
						}).catch((error) => {
							wx.showToast({
								title: '确认收款失败，请重试',
								icon: 'none',
								duration: 2000
							});
						}).finally(() => {
							wx.hideLoading();
						});
					} else if (res.cancel) {
						wx.showToast({
							title: '您已选择取消收款！',
							icon: 'none'
						});
					}
				}
			});
		},

		business(id) {
			this.confirmReceiveMoney(id, '/bus/index/confirmReceiveMoney');
		},

		store(id) {
			this.confirmReceiveMoney(id, '/store/my/confirmReceiveMoney');
		},

		server(id) {
			this.confirmReceiveMoney(id, '/waiter/my/confirmReceiveMoney');
		},

		city(id) {
			this.confirmReceiveMoney(id, '/city/index/confirmReceiveMoney');
		},

		cause(id) {
			this.confirmReceiveMoney(id, '/work/index/confirmReceiveMoney');
		}


	}
}